package com.cci.kangdao.controller;

import com.cci.kangdao.service.LoginService;
import com.cci.kangdao.utilTool.JsonUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.wechatOrder.exception.NotCheckException;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
 * @Description: 用户注册、登录
 * @Company: CTY
 * @author 米艳春
 * @date 2018-01-18
 * @version 1.0
 */
@Controller
public class Login {
	private Logger log = Logger.getLogger(this.getClass().getName());

	@Autowired
	private LoginService loginService;


	/**
	 * App注册用户接口
	 * @param map
	 * @return
	 */
	@RequestMapping("/userRegister.aspx")
	@Deprecated
	public ModelAndView userRegister(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			return loginService.userRegister(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}

	/**
	 * 此方法已经废弃，正常的登录是 调用微软的 登录页面，登录成功后  回调  common的接口
	 * App用户登录接口
	 *
	 * @param map
	 * @return
	 */
	@RequestMapping("/login.aspx")
	@Deprecated
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			return loginService.login(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * App用户验证token接口
	 * @param map
	 * @return
	 */
	@RequestMapping("/verifyToken.aspx")
	public ModelAndView verifyToken(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return loginService.verifyToken(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * App用户修改密码接口
	 * @param map
	 * @return
	 */
	@RequestMapping("/updateUserpwd.aspx")
	public ModelAndView updateUserpwd(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return loginService.updateUserpwd(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	
	/**
	 * 提供给MP密码修改功能
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/mpUpdateUserPassword.aspx")
	public ModelAndView mpUpdateUserPassword(@RequestBody String jsonParam,HttpServletRequest request, HttpServletResponse response){
		try{
			log.info("接口传参为"+jsonParam);
			Map<String,Object> map = JsonUtils.toJavaObject(jsonParam, Map.class);
			map.put("request", request);
			map.put("response", response);
			
			loginService.mpUpdateUserPassword(map);
		}catch(Exception ex){
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}
	
	
	
	
	/**
	 * 找回密码功能
	 * @param request header中需包含userId，参数包含手机号和密码
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findPassword.aspx")
	public ModelAndView findPassword(HttpServletRequest request,
									   HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return loginService.findPassword(map);
		}catch(Exception ex){
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}
	
	
	
	
	
	/**
	 * App用户获取首页功能
	 * @param map
	 * @return
	 */
	@RequestMapping("/index.aspx")
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return loginService.index(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}


	/**
	 * npbu项目接口     npbu根据角色获取e路康明斯app上npbu首页菜单（轮播图，功能项，文章分类）
	 * @param
	 * @return
	 */
	@RequestMapping("/npbuIndexMenu.aspx")
	public ModelAndView npbuIndexMenu(HttpServletRequest request,
							  HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return loginService.npbuIndexMenu(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 * npbu项目接口     npbu获取用户的所有动作
	 * @param
	 * @return
	 */
	@RequestMapping("/getNpbuAction.aspx")
	public ModelAndView getNpbuAction(HttpServletRequest request,
									  HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return loginService.getNpbuActionByUserid(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);

			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	/**
	 *web请求删除redis中的key
	 * @param map
	 * @return
	 */
	@RequestMapping("/delRediskey.aspx")
	public ModelAndView delRediskey(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			return loginService.delRedisKey(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	@RequestMapping("/getApiHost.aspx")
	@ResponseBody
	public String getApiHost(HttpServletRequest request,
							 HttpServletResponse response) {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return loginService.getApiDomain(map);
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage(),e);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	/**
	 * 客户端获取AD登录页面的接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getLoginUrl.aspx")
	public ModelAndView getGetLoginUrl(HttpServletRequest request,
									HttpServletResponse response) throws Exception {
		
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return loginService.getLoginUrl(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}
	
	
	

	@RequestMapping(value = "/initInfo.aspx")
	public void initInfo(HttpServletRequest request,
									   HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			loginService.initInfo(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
	}

	/**
	 * App用户注册
	 * 2021-05-18 增加了Guidanz用户的注册
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/register.aspx")
	public String register(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			loginService.register(map);
		}catch (Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}


	
	
	
	
	@RequestMapping(value = "/silentLogin.aspx")
	public String silentLogin(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			loginService.silentLogin(map);
		}catch (Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

	@RequestMapping(value = "/bindWWID.aspx")
	public void bindWWID(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			loginService.bindWWID(map);
		}catch (Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
	}

	@RequestMapping(value = "/bindPromotionId.aspx")
	public void bindPromotionId(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			loginService.bindPromotionId(map);
		}catch (Exception ex){
			log.error(ex.getMessage(),ex);
			ParameterTool.writeErrorResponse(response);
		}
	}

	@RequestMapping(value = "/getSystemTime.aspx")
	public void getSystemTime(HttpServletRequest request,HttpServletResponse response){
		try{

			JSONObject js = new JSONObject();
			js.put("systemTime", LocalTimeUtil.getRealCurrentTime());

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("status", 0);
			jsonObject.put("msg", "服务时间获取成功");
			jsonObject.put("data", js);
			// 返回客户
			ParameterTool.writeResponse(response, jsonObject.toString());

		}catch (Exception ex){

			log.error(ex.getMessage(),ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);

		}
	}

	/**
	 * 用户批量作废接口
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/user/delete.aspx")
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
		map.put("request", request);
		map.put("response", response);
		boolean success = loginService.delete(map);
		ParameterTool.writeSuccessResponse(response, success);
		return null;
	}

	/**
	 * app用户退出登录接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/appLoginOut.aspx")
	public void appLoginOut(HttpServletRequest request, HttpServletResponse response) {
		try {
			boolean success = loginService.appLoginOut(request,response);
			ParameterTool.writeSuccessResponse(response, success);
		} catch (Exception ex) {
			String msg = null;
			if (ex instanceof NotCheckException) {
				msg = "{\"status\": -99,\"msg\": \"" + ex.getMessage() + "\"}";
			} else {
				msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			}
			log.error(ex.getMessage());
			ParameterTool.writeResponse(response, msg);
		}
	}

}
